<?php

class Document_model extends CI_Model{
    
    public $table = 'document';
    
    function __construct(){
        parent::__construct();
        $this->load->helper('url');
        $this->load->database();
        
    }
    
    function create($data){
        $result = $this->db->insert($this->table, $data); 
        if($result) return true;
        else return false;
    }
    
    function list_all($number, $offset, $sort){
        $this->db->order_by($sort,'DESC');
        $query =  $this->db->get($this->table,$number,$offset); 
        return $query->result_array(); 
    } 
    // đếm tổng số record trong table book 
    function count_all(){ 
        //$this->db->where('MaYTuong = 4');
        return $this->db->count_all($this->table); 
    }
    
    function getById($id){
        $query =  $this->db->get_where($this->table, 'MaYTuong = '.$id);
        return $query->result();
    } 
    
    function getCommentById($id){
        $query =  $this->db->get_where('binhluandanhgia', 'MaYTuong = '.$id);
        return $query->result();
    } 
    function addComment($data, $diem){
        $result = $this->db->insert('binhluandanhgia', $data);
        $query = $this->db->query("UPDATE ".$this->table." SET  DiemDanhGia =  '".$diem."' WHERE  MaYTuong =".$data['MaYTuong']); 
        if($result) return true;
        else return false;
    }
    
    function list_sort($number, $offset, $id, $sort = 'Ngay'){
        $this->db->order_by($sort,'DESC');
        $query =  $this->db->get_where($this->table,'MaHoSo = '.$id,$number,$offset); 
        return $query->result(); 
    }
    
    function count_id($id){
        $query =  $this->db->get_where($this->table,'MaHoSo = '.$id); 
        return count($query->result_array()); 
    }
    
}

?>